package com.lync.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lync.domain.ExamPaper;
import com.lync.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exams")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    @GetMapping
    public Result getAll() {
        List<ExamPaper> examList = examPaperService.getAll();
        Integer code = examList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = examList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,examList,msg);
    }

    @PostMapping
    public Result save(@RequestBody ExamPaper examPaper) {
        boolean flag =examPaperService.save(examPaper);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PutMapping
    public Result update(@RequestBody ExamPaper examPaper) {
        boolean flag = examPaperService.modify(examPaper);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag =examPaperService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        ExamPaper examPaper = examPaperService.getById(id);
        Integer code =examPaper != null ? Code.GET_OK : Code.GET_ERR;
        String msg =examPaper != null ? "" : "数据查询失败，请重试！";
        return new Result(code,examPaper,msg);
    }



    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, ExamPaper examPaper){

        IPage<ExamPaper> page = examPaperService.getPage(currentPage, pageSize,examPaper);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = examPaperService.getPage((int)page.getPages(), pageSize,examPaper);
        }
        return new Result(true? Code.PAGE_OK:Code.PAGE_ERR,page);
    }

}
